mui.init({});

mui.plusReady(function(){
	loadDatas();
	
});

function loadDatas(){
	var self = plus.webview.currentWebview();
	var formId = self.formId;
	var sessionId =	plus.storage.getItem(KeyCfg.JSESSIONID);
	var url = UrlConfig.invest_record;
	//var formData = $("#myform").serialize();
	var formData = {transferLoanId:formId,sessionId:sessionId};
	MuiEventMgr.ajax(url,{pars:formData,
	  	sfn : function(data){
	  		callback(data);
	  	},ffn : function(err){
	  		mui.alert("获取数据失败：["+err+"]");
	  	}
	 });
}

function callback(data){	
	var loanRecords = data.loanRecords;
	if(typeof(loanRecords) == 'undefined'){
		$(".investRecord-header").css("display","none");
		$(".investRecord-info").css("display","none");
		$(".investRecord-table").css("display","none");
		$(".mui-table-view").css("display","");
		$("#noRecord1").css("display","");
	
		return;
	}
	var htmlArr = [];
	for(var i=0,count=loanRecords.length; i<count; i++){
		var loanRecord = loanRecords[i];
		var html = getHtml(loanRecord);
		htmlArr[i] = html;
	}
	var htmlCodes = htmlArr.join("  ");
	$("#investRecord-info").html(htmlCodes);

}

function getHtml(loanRecord){
	var loanTitle = loanRecord.loanTitle;
	var title = loanTitle.replace('XR','XR<br/>');
	var tenderTime = loanRecord.tenderTime;
	var YMD = tenderTime.substr(0,10);
	var state = loanRecord.state;
	var paidPeriod = loanRecord.paidPeriod;
	var totalPeriod = loanRecord.totalPeriod;
	var tenderMoney = loanRecord.tenderMoney;
	var rateYear = loanRecord.rateYear;
	var mark = loanRecord.mark;
	var loanId = loanRecord.loanId;
	

var htmls = [
		'<table class="investRecord-table" border="0">',
				'<tbody>',
					'<tr>',
						'<td class="date" >'+YMD+'</td>',
						'<td class="loanTitle" ><a style="color: black;" id="'+loanId+'" href="javascript:void(0)" onclick="openWindow(\''+title+'\',\''+tenderMoney+'\',\''+rateYear+'\',\''+YMD+'\',\''+paidPeriod+'\',\''+totalPeriod+'\');" >'+'<u>'+title+'</u>'+'</a></td>',
						
						'<td class="state" >'+state+'</td>',
						'<td class="refundDeadline">'+paidPeriod+'/'+totalPeriod+'</td>',
					'</tr>',
				'</tbody>',
			'</table>'
			];
	return htmls.join(" "); 
}


function openWindow(title,tenderMoney,rateYear,YMD,paidPeriod,totalPeriod){
			var pars = {title:title, tenderMoney:tenderMoney, rateYear:rateYear, YMD:YMD, paidPeriod:paidPeriod, totalPeriod:totalPeriod};
			var cfg = {pars : pars};
			MuiEventMgr.open('investInfo.html',cfg);	
}


mui(".mui-popover ").on("tap", "a", function() {
	var targetPageId = plus.webview.currentWebview().id;
	plus.storage.setItem(KeyCfg.targetPageId,targetPageId);
	var self = this;
	var name=$(self).attr("id");
	switch(name){
		case 'invest_all':
		     loadDatas();
		     mui('#Popover').popover('hide');
		      $("#Popover").css("display","none");
		      $(".investRecord-header").css("display","");
		      $(".investRecord-info").css("display","");
		      $(".investRecord-table").css("display","");
		      //$(".mui-table-view").css("display","none");
		      $("#noRecord1").css("display","none");
			break;
		case 'invest_one':
		     $("#invest0").removeClass("activity");
		     $("#invest1").addClass("activity");
		     $("#invest3").removeClass("activity");
		     $("#invest4").removeClass("activity");
		     $("#invest2").removeClass("activity");
		      investOne();
		      mui('#Popover').popover('hide');
		      $("#Popover").css("display","none");
		      $(".investRecord-header").css("display","");
		      $(".investRecord-info").css("display","");
		      $(".investRecord-table").css("display","");
		      $("#noRecord1").css("display","none");
			break;
		case 'invest_two':
		      $("#invest0").removeClass("activity");
		      $("#invest1").removeClass("activity");
		      $("#invest4").removeClass("activity");
		      $("#invest3").removeClass("activity");
		      $("#invest2").addClass("activity");
		      investTwo();
		      mui('#Popover').popover('hide');
		      $("#Popover").css("display","none");
		      $(".investRecord-header").css("display","");
		      $(".investRecord-info").css("display","");
		      $(".investRecord-table").css("display","");
		      $("#noRecord1").css("display","none");
			break;
		case 'invest_three':
		$("#invest0").removeClass("activity");
		      $("#invest1").removeClass("activity");
		      $("#invest2").removeClass("activity");
		      $("#invest0").removeClass("activity");
		      $("#invest3").addClass("activity");
		      $("#invest4").removeClass("activity");
		     investThree();
		      $("#Popover").css("display","none");
		      mui('#Popover').popover('hide');
		      
		      $(".investRecord-header").css("display","");
		      $(".investRecord-info").css("display","");
		      $(".investRecord-table").css("display","");
		      $("#noRecord1").css("display","none");
			break;
		case 'invest_four':
		      $("#invest0").removeClass("activity");
		      $("#invest1").removeClass("activity");
		      $("#invest2").removeClass("activity");
		      $("#invest3").removeClass("activity");
		      $("#invest4").addClass("activity");
		     investFour();
		      $("#Popover").css("display","none");
		      mui('#Popover').popover('hide');
		      
		      $(".investRecord-header").css("display","");
		      $(".investRecord-info").css("display","");
		      $(".investRecord-table").css("display","");
		      $("#noRecord1").css("display","none");
			break;
	}
});

function investOne(){
	var self = plus.webview.currentWebview();
	var formId = self.formId;
	var sessionId =	plus.storage.getItem(KeyCfg.JSESSIONID);
	var url = UrlConfig.invest_one;
	//var formData = $("#myform").serialize();
	var formData = {transferLoanId:formId,sessionId:sessionId};
	MuiEventMgr.ajax(url,{pars:formData,
	  	sfn : function(data){
	  		callback(data);
	  	},ffn : function(err){
	  		mui.alert("获取数据失败：["+err+"]");
	  	}
	 });
}

function investTwo(){
	var self = plus.webview.currentWebview();
	var formId = self.formId;
	var sessionId =	plus.storage.getItem(KeyCfg.JSESSIONID);
	var url = UrlConfig.invest_two;
	//var formData = $("#myform").serialize();
	var formData = {transferLoanId:formId,sessionId:sessionId};
	MuiEventMgr.ajax(url,{pars:formData,
	  	sfn : function(data){
	  		callback(data);
	  	},ffn : function(err){
	  		mui.alert("获取数据失败：["+err+"]");
	  	}
	 });
}


function investThree(){
	var self = plus.webview.currentWebview();
	var formId = self.formId;
	var sessionId =	plus.storage.getItem(KeyCfg.JSESSIONID);
	var url = UrlConfig.invest_three;
	var formData = {transferLoanId:formId,sessionId:sessionId};
	MuiEventMgr.ajax(url,{pars:formData,
	  	sfn : function(data){
	  		callback(data);
	  	},ffn : function(err){
	  		mui.alert("获取数据失败：["+err+"]");
	  	}
	 });
}

function investFour(){
	var self = plus.webview.currentWebview();
	var formId = self.formId;
	var sessionId =	plus.storage.getItem(KeyCfg.JSESSIONID);
	var url = UrlConfig.invest_four;
	//var formData = $("#myform").serialize();
	var formData = {transferLoanId:formId,sessionId:sessionId};
	MuiEventMgr.ajax(url,{pars:formData,
	  	sfn : function(data){
	  		callback(data);
	  	},ffn : function(err){
	  		mui.alert("获取数据失败：["+err+"]");
	  	}
	 });
}

function investOne(){
	var self = plus.webview.currentWebview();
	var formId = self.formId;
	var sessionId =	plus.storage.getItem(KeyCfg.JSESSIONID);
	var url = UrlConfig.invest_one;
	var formData = {transferLoanId:formId,sessionId:sessionId};
	MuiEventMgr.ajax(url,{pars:formData,
	  	sfn : function(data){
	  		callback(data);
	  	},ffn : function(err){
	  		mui.alert("获取数据失败：["+err+"]");
	  	}
	 });
}














